﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Common.Helpers/ConfigHelper 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       ConfigHelper
* 创建时间：  2025/5/22 15:50:06
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.Common.Helpers
{
    public static class ConfigHelper
    {
        #region appsetting
        /// <summary>
        /// 获取AppSetting值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetAppSetting(string key, string defaultValue = "")
        {

            return AppContextProvider.Current.AppSettings.GetValue<string>(key) ?? defaultValue;

        }

        /// <summary>
        /// 获取AppSetting值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetAppSettingInt(string key)
        {
            return GetAppSetting(key).ToInt();
        }

        /// <summary>
        /// 获取AppSetting值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetAppSettingBoolean(string key)
        {
            return GetAppSetting(key).ToBool();
        }

        /// <summary>
        /// 获取AppSetting值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal GetAppSettingDecimal(string key, decimal defaultValue = 0M)
        {
            return GetAppSetting(key).ObjToDecimal(defaultValue);
        }

        /// <summary>
        /// 获取AppSetting值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long GetAppSettingLong(string key)
        {
            return GetAppSetting(key).ToLong();
        }
        #region Options
        /// <summary>
        /// 获得选项
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static TOptions GetOptions<TOptions>(string path) where TOptions : class, new() =>
           AppContextProvider.Current.Configuration.GetSection(path).Get<TOptions>();

        /// <summary>
        /// 获得选项
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="serviceProvider"></param>
        /// <returns></returns>
        public static TOptions GetOptions<TOptions>() where TOptions : class, new() =>
            AppDependencyResolver.Current.GetService<IOptions<TOptions>>()?.Value;

        /// <summary>
        /// 获得选项
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="serviceProvider"></param>
        /// <returns></returns>
        public static TOptions GetOptionsMonitor<TOptions>() where TOptions : class, new() =>
            AppDependencyResolver.Current.GetService<IOptionsMonitor<TOptions>>()?.CurrentValue;

        /// <summary>
        /// 获得选项
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="serviceProvider"></param>
        /// <returns></returns>
        public static TOptions GetOptionsSnapshot<TOptions>() where TOptions : class, new() =>
            AppDependencyResolver.Current.GetService<IOptionsSnapshot<TOptions>>()?.Value;
        #endregion
        #endregion
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="environmentName">环境名称</param>
        /// <param name="optional">可选</param>
        /// <param name="reloadOnChange">自动更新</param>
        /// <returns></returns>
        public static IConfiguration Load(string fileName, string environmentName = "", bool optional = true, bool reloadOnChange = false)
        {
            var filePath = Path.Combine(AppContext.BaseDirectory, "Configs");
            if (!Directory.Exists(filePath))
                return null;

            var builder = new ConfigurationBuilder()
                .SetBasePath(filePath)
                .AddJsonFile(fileName + ".json", optional, reloadOnChange);

            if (environmentName.IsNotNullOrEmpty())
            {
                builder.AddJsonFile(fileName + "." + environmentName + ".json", optional: optional, reloadOnChange: reloadOnChange);
            }

            return builder.Build();
        }
        /// <summary>
        /// 获得配置信息
        /// </summary>
        /// <typeparam name="T">配置信息</typeparam>
        /// <param name="fileName">文件名称</param>
        /// <param name="environmentName">环境名称</param>
        /// <param name="optional">可选</param>
        /// <param name="reloadOnChange">自动更新</param>
        /// <returns></returns>
        public static T Get<T>(string fileName, string environmentName = "", bool optional = true, bool reloadOnChange = false)
        {
            var configuration = Load(fileName, environmentName, optional, reloadOnChange);
            if (configuration == null)
                return default;

            return configuration.Get<T>();
        }
        /// <summary>
        /// 绑定实例配置信息
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="instance">实例配置</param>
        /// <param name="environmentName">环境名称</param>
        /// <param name="optional">可选</param>
        /// <param name="reloadOnChange">自动更新</param>
        public static void Bind(string fileName, object instance, string environmentName = "", bool optional = true, bool reloadOnChange = false)
        {
            var configuration = Load(fileName, environmentName, optional, reloadOnChange);
            if (configuration == null || instance == null)
                return;

            configuration.Bind(instance);
        }
        public static string GetContent(params string[] sections)
        {
            try
            {
                if (sections.Any())
                { 
                    return AppContextProvider.Current.Configuration[string.Join(":", sections)];
                }
            }
            catch (Exception) { }

            return "";
        }
    }
}
